package com.xinsilu.hospital.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.xinsilu.hospital.entity.*;
import com.xinsilu.hospital.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import java.awt.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
public class WxController {

    @Autowired
    private IInformationTableService informationTableService;//院区列表
    @Autowired
    private IDepartmentTableService iDepartmentTableService;//科室列表
    @Autowired
    private WxControllerService wxControllerService; //获取信息
    @Autowired
    private IDoctorTableService iDoctorTableService;


    @Autowired
    private IUserTableService iUserTableService;
    @Autowired
    private  IScheduleService iScheduleService;

    @Autowired
    private IRegistrationTableService iRegistrationTableService;  
    @PostMapping("/WxGetRegister")
    public Object WxGetRegister(HttpServletRequest request){

        List<InformationTable> list=informationTableService.list();
        System.out.println(list);

        return  list;
    }
    @PostMapping("/WxGetHospitlName")
    public String WxGetHospitlName(HttpServletRequest request){
        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println(Info);
        String id=Info.getString("id");
        InformationTable informationTable= informationTableService.getById(id);
        String hospitalName=informationTable.getState();

        System.out.println(hospitalName);
        return hospitalName;
    }


    @PostMapping("/WxGetadministrative")
    public Object WxGetadministrative(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println(Info);
        String id=Info.getString("id");

        Map<String,Object> map1=new HashMap<>();
        map1.put("suoshuyuanqu",id);
        List<DepartmentTable> lists=iDepartmentTableService.listByMap(map1);
        System.out.println(lists);

        return  lists;
    }

    @PostMapping("/WxGetDocator")
    public Object WxGetDocator(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);
        DoctorTable doctorTable=new DoctorTable();
        DepartmentTable departmentTable=new DepartmentTable();
        Schedule schedule=new Schedule();
        departmentTable.setSuoshuyuanqu(Info.getString("yuanquid"));
        departmentTable.setName(Info.getString("keshiname"));
        doctorTable.setDepartmentTable(departmentTable);
        schedule.setDate(Info.getString("date"));
        doctorTable.setSchedule(schedule);

        List<DoctorTable> list=  wxControllerService.getDoctor(doctorTable);

//        JSON.toJSONString(list);

        System.out.println(JSON.toJSONString(list));


        return JSON.toJSONString(list);
    }

    @PostMapping("/Wxgetiddoctor")
    public Object Wxgetiddoctor(HttpServletRequest request){
        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        String id=Info.getString("id");
        Map<String,Object> map=new HashMap<>();
        map.put("id",id);
        DoctorTable doctorTable=new DoctorTable();
        doctorTable.setId(Integer.parseInt(id));
        List<DoctorTable> list=wxControllerService.getidDoctor(doctorTable);
//        String id=Info.getString("id");
////        InformationTable informationTable= informationTableService.getById(id);
////        String hospitalName=informationTable.getState();
////
////        System.out.println(hospitalName);
////        return hospitalName;
        return  list;
    }

    @PostMapping("/Wxgetjiuzhenren")
    public Object Wxgetjiuzhenren(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);

        String openid=Info.getString("openid");

        Map<String,Object> map1=new HashMap<>();
        map1.put("openid",openid);
        List<UserTable> lists=iUserTableService.listByMap(map1);
        System.out.println(lists);

        return  lists;
    }

    @PostMapping("/WxgetDoctorTime")
    public Object WxgetDoctorTime(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);

        String id=Info.getString("id");
        String date=Info.getString("date");
        Map<String,Object> map1=new HashMap<>();
        map1.put("doctor_id",id);
        map1.put("date",date);
        List<Schedule> list=iScheduleService.listByMap(map1);
        if (list.size()==0||null==list){

            return  true;
        }
        else {
            return false;
        }


    }
    @RequestMapping("/addguahao")
    public Object addguahao(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);

        RegistrationTable registrationTable=null;
        String date=Info.getString("jiuzhenriqi");
        String card=Info.getString("bingrencard");
        String departmentname=Info.getString("keshiname");
        String informationId=Info.getString("yuanquid");
        String Telephone=Info.getString("phone");
        Map<String,Object> map1=new HashMap<>();
        map1.put("date",date);
        map1.put("card",card);
        map1.put("departmentname",departmentname);
        map1.put("information_id",informationId);
        map1.put("Telephone",Telephone);
        List<RegistrationTable> list=iRegistrationTableService.listByMap(map1);

        if (list.size()==0||null==list){
            registrationTable=new RegistrationTable();
            registrationTable.setDepartmentname(Info.getString("keshiname"));
            registrationTable.setDate(Info.getString("jiuzhenriqi"));
            registrationTable.setCard(Info.getString("bingrencard"));
            registrationTable.setCost(Info.getString("cost"));
            registrationTable.setTelephone(Info.getString("phone"));
            registrationTable.setName(Info.getString("bingrenname"));
            registrationTable.setShiduan(Info.getString("shiduan"));
            registrationTable.setType(Info.getString("position"));
            registrationTable.setDoctorname(Info.getString("yishengname"));
            registrationTable.setOpenid(Info.getString("openid"));
            registrationTable.setAge(23);
            registrationTable.setInformationId(Integer.parseInt(Info.getString("yuanquid")));
            boolean b= iRegistrationTableService.save(registrationTable);
            if (b){
                return  true;
            }else {
                return  false;
            }
        }else{
            return false;
        }






    }


    @PostMapping("/deleteguahao")
    public Object deleteguahao(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);
        String date=Info.getString("jiuzhenriqi");
        String card=Info.getString("bingrencard");
        String departmentname=Info.getString("keshiname");
        String informationId=Info.getString("yuanquid");
        String Telephone=Info.getString("phone");

        Map<String,Object> map1=new HashMap<>();
        map1.put("date",date);
        map1.put("card",card);
        map1.put("departmentname",departmentname);
        map1.put("informationId",informationId);
        map1.put("Telephone",Telephone);
       boolean f= iRegistrationTableService.removeByMap(map1);
        return  f;
    }
    @PostMapping("/addjiuzhenren")
    public Object addjiuzhenren(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);
        UserTable userTable=new UserTable();
        userTable.setOpenid(Info.getString("openid"));
        userTable.setName(Info.getString("name"));
        userTable.setCard(Info.getString("card"));
        userTable.setTelephone(Info.getString("phone"));
        Map<String,Object> map1=new HashMap<>();
        map1.put("card",Info.getString("card"));
        map1.put("openid",Info.getString("openid"));
        List<UserTable> list=iUserTableService.listByMap(map1);
        if(list.size()==0||null==list){
            boolean b=iUserTableService.save(userTable);
            if (b){
                return  true;
            }else {
                return  false;
            }
        }else {
            return  false;
        }



    }

    @PostMapping("/getjiuzhenrenx")
    public Object getjiuzhenrenx(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);

        String id=Info.getString("id");

        Map<String,Object> map1=new HashMap<>();
        map1.put("id",id);
        List<UserTable> lists=iUserTableService.listByMap(map1);
        System.out.println(lists);

        return  lists;
    }

    @PostMapping("/updateren")
    public Object updateren(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);

//        String id=Info.getString("id");
//
//        Map<String,Object> map1=new HashMap<>();
//        map1.put("id",id);
//        List<UserTable> lists=iUserTableService.listByMap(map1);
//        System.out.println(lists);

        return  "lists";
    }


    @PostMapping("/getguahaojilu")
    public Object getguahaojilu(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);
        Map<String,Object> map1=new HashMap<>();
        map1.put("openid",Info.getString("openid"));
       List<RegistrationTable>list= iRegistrationTableService.listByMap(map1);

        return  list;
    }


    @PostMapping("/getjili")
    public Object getjili(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);
        Map<String,Object> map1=new HashMap<>();
         String id=Info.getString("id");
         map1.put("id",id);
        List<RegistrationTable>list= iRegistrationTableService.listByMap(map1);

        return  list;
    }

    @PostMapping("/deleteguahao2")
    public Object deleteguahao2(HttpServletRequest request){

        String info=request.getParameter("info");
        JSONObject Info=JSON.parseObject(info);
        System.out.println("接受到的参数"+Info);
        boolean f= iRegistrationTableService.removeById(Info.getString("id"));
        return  f;
    }

}
